// 云函数入口文件
const cloud = require('wx-server-sdk')
const superagent = require('superagent');
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  return new Promise((resolve, reject) => {
    const baseUrl = 'http://jxfw.gdut.edu.cn/new/login'
    let formData = {
      account: event.idName, //'3114006472'
      pwd: event.passwd, //'472040743lzh'
      verifycode: event.yzm,
    }
    // console.log(formData)
    superagent.post(baseUrl)
      .set("Cookie", event.cookie)
      .set("Referer","http://jxfw.gdut.edu.cn/")
      // .set("User - Agent","Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 74.0.3729.131 Safari / 537.36")
      .type("form")
      .send(formData)
      .end(function (err, res) {
        if (err) {
          reject(err);
        };
        resolve(res)
        // let result = JSON.parse(res.text)
        // if (result.code == 0) {
        //   // console.log('result' + result.code)

        //   resolve(result.code)
        // } else {
        //   // console.log(result);
        //   console.log('fail to login:')
        //   resolve(1)
        // }
      }) //post end函数
  }) //return
}